package actions;

import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import utils.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HistoryAction extends CMActionSupport {
	private Logger logger = LogManager.getLogger(this);
	private String id = "";
	private int type = 2; //前端提交待审核数据接口的请求类型,默认为2说明前端没有请求type
	private String passapplyId; //前端提交待审核数据接口的 id 需要在后端转换成JSON

	public String my_waiting() {
		logger.debug("请求我的预约——待审核数据");
		JSONObject result = new JSONObject();
		final List<JSONObject> jsonList = new ArrayList<>();
		String sql = "get_borrowlog_conditional"; //查找某个id的待审核数据
		new DBSelect(sql, null, null, null, null, id, 1).execProcedure(
				new DBSelectInt() {

			@Override
			public boolean deal(ResultSet rs) throws SQLException {
				while (rs.next()) {
//					select `building`, `room`, `borrowdate`, `borrowtime`, `userid`, `status`
					JSONObject tmpJson = new JSONObject();
					tmpJson.put("apply_id", rs.getString("id")); //参数可以是列的序号，也可以是列名
					tmpJson.put("user_id", rs.getString("userid"));
					tmpJson.put("apply_classroom_id", rs.getString("building") + "-" + rs.getString("room"));
					tmpJson.put("apply_date", rs.getString("applytime"));
					String borrowTime = convertBorrowTime(Integer.valueOf(rs.getString("borrowtime")));
					tmpJson.put("user_date", rs.getString("borrowdate") + " " + borrowTime);
					tmpJson.put("purpose", rs.getString("reason"));
					tmpJson.put("apply_status", "待审核");
					jsonList.add(tmpJson);

				}
				return false;
			}
		});
		new DBSelect(sql, null, null, null, null, id, 4).execProcedure(
				new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						while (rs.next()) {
//					select `building`, `room`, `borrowdate`, `borrowtime`, `userid`, `status`
							JSONObject tmpJson = new JSONObject();
							tmpJson.put("apply_id", rs.getString("id")); //参数可以是列的序号，也可以是列名
							tmpJson.put("user_id", rs.getString("userid"));
							tmpJson.put("apply_classroom_id", rs.getString("building") + "-" + rs.getString("room"));
							tmpJson.put("apply_date", rs.getString("applytime"));
							String borrowTime = convertBorrowTime(Integer.valueOf(rs.getString("borrowtime")));
							tmpJson.put("user_date", rs.getString("borrowdate") + " " + borrowTime);
							tmpJson.put("purpose", rs.getString("reason"));
							tmpJson.put("apply_status", "已拒绝");
							jsonList.add(tmpJson);

						}
						return false;
					}
				});
		result.put("tabledata",jsonList);
		JSONResponse.returnJsonToResponse(result);
		return NONE;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return this.id;
	}


	public String my_passed() {
		logger.debug("请求我的预约——已通过数据");
		JSONObject result = new JSONObject();
		final List<JSONObject> jsonList = new ArrayList<>();
		String sql = "get_borrowlog_conditional"; //查找某个id的待审核数据
		new DBSelect(sql, null, null, null, null, id, 3).execProcedure(
				new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						while (rs.next()) {
//`name`, `building`, `room`, `borrowdate`, `borrowtime`, `userid`, `reason`, `status`, `applytime`, `passtime`
							JSONObject tmpJson = new JSONObject();
							tmpJson.put("apply_id", rs.getString("id")); //参数可以是列的序号，也可以是列名
							tmpJson.put("user_id", rs.getString("userid"));
							tmpJson.put("apply_classroom_id", rs.getString("building") + "-" + rs.getString("room"));
							tmpJson.put("apply_date", rs.getString("applytime"));
							String borrowTime = convertBorrowTime(Integer.valueOf(rs.getString("borrowtime")));
							tmpJson.put("user_date", rs.getString("borrowdate") + " " + borrowTime);
							tmpJson.put("purpose", rs.getString("reason"));
							tmpJson.put("apply_status", "已通过");
							tmpJson.put("pass_time", rs.getString("passtime"));

							jsonList.add(tmpJson);

						}
						return false;
					}
				});
		result.put("tabledata", jsonList);
		JSONResponse.returnJsonToResponse(result);
		return NONE;
	}

	public String general_waiting() {
		logger.debug("请求 管理员——待审核数据");
		JSONObject result = new JSONObject();
		final List<JSONObject> jsonList = new ArrayList<>();

		if (type == 0) { //拒绝
			//解析传入的id passapplyId:" {"0":11111,"1":11111} " debug
			JSONObject ids = new JSONObject().fromObject(passapplyId);
			logger.info("转换的json为：" + ids.toString());
			Iterator<String> it = ids.keys();
			while (it.hasNext()) {
				// 获得key
				String key = it.next();
				String value = ids.getString(key);
				logger.info("key: " + key + ",value:" + value);
				//value即为学号userid
				String sql = "update borrowlog SET status = '4' where id = '" + value + "'";
				new DBSelect(sql).exec(new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						return false;
					}
				});
			}
			result.put("status", "success");
			JSONResponse.returnJsonToResponse(result);
			return NONE;


		}
		if (type == 1) { //通过 设置为2
			//解析传入的id passapplyId:" {"0":11111,"1":11111} " debug
			JSONObject ids = new JSONObject().fromObject(passapplyId);
			logger.info("转换的json为：" + ids.toString());
			Iterator<String> it = ids.keys();
			while (it.hasNext()) {
				// 获得key
				String key = it.next();
				String value = ids.getString(key);
				logger.info("key: " + key + ",value:" + value);
				//value即为学号userid
				String sql = "update borrowlog SET status = '2' where id = '" + value + "'";
				new DBSelect(sql).exec(new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						return false;
					}
				});
			}
			result.put("status", "success");
			JSONResponse.returnJsonToResponse(result);
			return NONE;
		}

		//前面如果设置了type就会执行上面的语句return 如果没有设置type说明是第一次刷新
		String sql = "get_borrowlog_conditional"; //查找某个id的待审核数据
		/*12345分别代表待审核、普通管理员已通过、高级管理员已通过、已拒绝、已取消'*/
		new DBSelect(sql, null, null, null, null, null, 1).execProcedure(
				new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						while (rs.next()) {
//`name`, `building`, `room`, `borrowdate`, `borrowtime`, `userid`, `reason`, `status`, `applytime`, `passtime`
							JSONObject tmpJson = new JSONObject();
							tmpJson.put("apply_id", rs.getString("id")); //参数可以是列的序号，也可以是列名
							tmpJson.put("user_id", rs.getString("userid"));
							tmpJson.put("apply_classroom_id", rs.getString("building") + "-" + rs.getString("room"));
							tmpJson.put("apply_date", rs.getString("applytime"));
							String borrowTime = convertBorrowTime(Integer.valueOf(rs.getString("borrowtime")));
							tmpJson.put("user_date", rs.getString("borrowdate") + " " + borrowTime);
							tmpJson.put("purpose", rs.getString("reason"));
							tmpJson.put("apply_status", "等待普通管理员审核");
							tmpJson.put("pass_time", rs.getString("passtime"));

							jsonList.add(tmpJson);

						}
						return false;
					}
				});
		result.put("tabledata", jsonList);
		JSONResponse.returnJsonToResponse(result);
		return NONE;
	}

	public String general_passed() {
		logger.debug("请求 管理员——已通过数据");
		JSONObject result = new JSONObject();
		final List<JSONObject> jsonList = new ArrayList<>();
		String sql = "get_borrowlog_conditional"; //查找某个id的待审核数据
		/*12345分别代表待审核、普通管理员已通过、高级管理员已通过、已拒绝、已取消'*/
		new DBSelect(sql, null, null, null, null, null, 2).execProcedure(
				new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						while (rs.next()) {
//`name`, `building`, `room`, `borrowdate`, `borrowtime`, `userid`, `reason`, `status`, `applytime`, `passtime`
							JSONObject tmpJson = new JSONObject();
							tmpJson.put("apply_id", rs.getString("id")); //参数可以是列的序号，也可以是列名
							tmpJson.put("user_id", rs.getString("userid"));
							tmpJson.put("apply_classroom_id", rs.getString("building") + "-" + rs.getString("room"));
							tmpJson.put("apply_date", rs.getString("applytime"));
							String borrowTime = convertBorrowTime(Integer.valueOf(rs.getString("borrowtime")));
							tmpJson.put("user_date", rs.getString("borrowdate") + " " + borrowTime);
							tmpJson.put("purpose", rs.getString("reason"));
							tmpJson.put("apply_status", "普通管理员 已通过");
							tmpJson.put("pass_time", rs.getString("passtime"));

							jsonList.add(tmpJson);

						}
						return false;
					}
				});
		new DBSelect(sql, null, null, null, null, id, 3).execProcedure(
				new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						while (rs.next()) {
//`name`, `building`, `room`, `borrowdate`, `borrowtime`, `userid`, `reason`, `status`, `applytime`, `passtime`
							JSONObject tmpJson = new JSONObject();
							tmpJson.put("apply_id", rs.getString("id")); //参数可以是列的序号，也可以是列名
							tmpJson.put("user_id", rs.getString("userid"));
							tmpJson.put("apply_classroom_id", rs.getString("building") + "-" + rs.getString("room"));
							tmpJson.put("apply_date", rs.getString("applytime"));
							String borrowTime = convertBorrowTime(Integer.valueOf(rs.getString("borrowtime")));
							tmpJson.put("user_date", rs.getString("borrowdate") + " " + borrowTime);
							tmpJson.put("purpose", rs.getString("reason"));
							tmpJson.put("apply_status", "高级管理员 已通过");
							tmpJson.put("pass_time", rs.getString("passtime"));

							jsonList.add(tmpJson);

						}
						return false;
					}
				});
		result.put("tabledata", jsonList);
		JSONResponse.returnJsonToResponse(result);
		return NONE;
	}

	public String advanced_waiting() {
		/*这里代码和普通管理员--待审核数据的处理方法差不多，直接把代码复制过来略改*/
		logger.debug("请求 高级管理员——待审核数据");
		JSONObject result = new JSONObject();
		final List<JSONObject> jsonList = new ArrayList<>();

		if (type == 0) { //拒绝
			//解析传入的id passapplyId:" {"0":11111,"1":11111} " debug
			JSONObject ids = new JSONObject().fromObject(passapplyId);
			logger.info("转换的json为：" + ids.toString());
			Iterator<String> it = ids.keys();
			while (it.hasNext()) {
				// 获得key
				String key = it.next();
				String value = ids.getString(key);
				logger.info("key: " + key + ",value:" + value);
				//value即为学号userid
				String sql = "update borrowlog SET status = '4' where id = '" + value + "'";
				new DBSelect(sql).exec(new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						return false;
					}
				});
			}
			result.put("status", "success");
			JSONResponse.returnJsonToResponse(result);
			return NONE;


		}
		if (type == 1) { //高级管理员通过 设置为3
			//解析传入的id passapplyId:" {"0":11111,"1":11111} " debug
			JSONObject ids = new JSONObject().fromObject(passapplyId);
			logger.info("转换的json为：" + ids.toString());
			Iterator<String> it = ids.keys();
			while (it.hasNext()) {
				// 获得key
				String key = it.next();
				String value = ids.getString(key);
				logger.info("key: " + key + ",value:" + value);
				//value即为学号userid
				String sql = "update borrowlog SET status = '3' where id = '" + value + "'";
				new DBSelect(sql).exec(new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						return false;
					}
				});
			}
			result.put("status", "success");
			JSONResponse.returnJsonToResponse(result);
			return NONE;
		}

		//前面如果设置了type就会执行上面的语句return 如果没有设置type说明是第一次刷新
		String sql = "get_borrowlog_conditional"; //查找某个id的待审核数据
		/*12345分别代表待审核、普通管理员已通过、高级管理员已通过、已拒绝、已取消'*/
		new DBSelect(sql, null, null, null, null, null, 2).execProcedure(
				new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						while (rs.next()) {
//`name`, `building`, `room`, `borrowdate`, `borrowtime`, `userid`, `reason`, `status`, `applytime`, `passtime`
							JSONObject tmpJson = new JSONObject();
							tmpJson.put("apply_id", rs.getString("id")); //参数可以是列的序号，也可以是列名
							tmpJson.put("user_id", rs.getString("userid"));
							tmpJson.put("apply_classroom_id", rs.getString("building") + "-" + rs.getString("room"));
							tmpJson.put("apply_date", rs.getString("applytime"));
							String borrowTime = convertBorrowTime(Integer.valueOf(rs.getString("borrowtime")));
							tmpJson.put("user_date", rs.getString("borrowdate") + " " + borrowTime);
							tmpJson.put("purpose", rs.getString("reason"));
							tmpJson.put("apply_status", "待审核，普通管理员已通过");
							tmpJson.put("pass_time", rs.getString("passtime"));

							jsonList.add(tmpJson);

						}
						return false;
					}
				});
		result.put("tabledata", jsonList);
		JSONResponse.returnJsonToResponse(result);
		return NONE;

	}

	public String advanced_passed() {
		logger.debug("请求 高级管理员——已通过数据");
		JSONObject result = new JSONObject();
		final List<JSONObject> jsonList = new ArrayList<>();
		String sql = "get_borrowlog_conditional"; //查找某个id的待审核数据
		/*12345分别代表待审核、普通管理员已通过、高级管理员已通过、已拒绝、已取消'*/
		new DBSelect(sql, null, null, null, null, null, 3).execProcedure(
				new DBSelectInt() {

					@Override
					public boolean deal(ResultSet rs) throws SQLException {
						while (rs.next()) {
//`name`, `building`, `room`, `borrowdate`, `borrowtime`, `userid`, `reason`, `status`, `applytime`, `passtime`
							JSONObject tmpJson = new JSONObject();
							tmpJson.put("apply_id", rs.getString("id")); //参数可以是列的序号，也可以是列名
							tmpJson.put("user_id", rs.getString("userid"));
							tmpJson.put("apply_classroom_id", rs.getString("building") + "-" + rs.getString("room"));
							tmpJson.put("apply_date", rs.getString("applytime"));
							String borrowTime = convertBorrowTime(Integer.valueOf(rs.getString("borrowtime")));
							tmpJson.put("user_date", rs.getString("borrowdate") + " " + borrowTime);
							tmpJson.put("purpose", rs.getString("reason"));
							tmpJson.put("apply_status", "高级管理员已通过");
							tmpJson.put("pass_time", rs.getString("passtime"));

							jsonList.add(tmpJson);

						}
						return false;
					}
				});
		result.put("tabledata", jsonList);
		JSONResponse.returnJsonToResponse(result);


		return NONE;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getPassapplyId() {
		return passapplyId;
	}

	public void setPassapplyId(String passapplyId) {
		this.passapplyId = passapplyId;
	}
}
